home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1I0OJE5 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  3.9 KB  |  107 lines

  1. package java.beans;
  2.  
  3. import java.util.Hashtable;
  4.  
  5. public class PropertyEditorManager {
  6.    private static String[] searchPath = new String[]{"sun.beans.editors"};
  7.    private static Hashtable registry;
  8.  
  9.    public static PropertyEditor findEditor(Class targetType) {
  10.       initialize();
  11.       Class editorClass = (Class)registry.get(targetType);
  12.       if (editorClass != null) {
  13.          try {
  14.             Object o = editorClass.newInstance();
  15.             return (PropertyEditor)o;
  16.          } catch (Exception var7) {
  17.             System.err.println("Couldn't instantiate type editor \"" + editorClass.getName() + "\" : " + var7);
  18.          }
  19.       }
  20.  
  21.       String editorName = targetType.getName() + "Editor";
  22.  
  23.       try {
  24.          return instantiate(targetType, editorName);
  25.       } catch (Exception var6) {
  26.          for(editorName = targetType.getName(); editorName.indexOf(46) > 0; editorName = editorName.substring(editorName.indexOf(46) + 1)) {
  27.          }
  28.  
  29.          for(int i = 0; i < searchPath.length; ++i) {
  30.             String name = searchPath[i] + "." + editorName + "Editor";
  31.  
  32.             try {
  33.                return instantiate(targetType, name);
  34.             }
  35.          }
  36.  
  37.          return null;
  38.       }
  39.    }
  40.  
  41.    public static String[] getEditorSearchPath() {
  42.       return searchPath;
  43.    }
  44.  
  45.    private static synchronized void initialize() {
  46.       if (registry == null) {
  47.          registry = new Hashtable();
  48.          load(Byte.TYPE, "ByteEditor");
  49.          load(Short.TYPE, "ShortEditor");
  50.          load(Integer.TYPE, "IntEditor");
  51.          load(Long.TYPE, "LongEditor");
  52.          load(Boolean.TYPE, "BoolEditor");
  53.          load(Float.TYPE, "FloatEditor");
  54.          load(Double.TYPE, "DoubleEditor");
  55.       }
  56.    }
  57.  
  58.    private static PropertyEditor instantiate(Class sibling, String className) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
  59.       ClassLoader cl = sibling.getClassLoader();
  60.       if (cl != null) {
  61.          try {
  62.             Class cls = cl.loadClass(className);
  63.             Object o = cls.newInstance();
  64.             PropertyEditor ed = (PropertyEditor)o;
  65.             return ed;
  66.          } catch (Exception var6) {
  67.          }
  68.       }
  69.  
  70.       Class cls = Class.forName(className);
  71.       Object o = cls.newInstance();
  72.       PropertyEditor ed = (PropertyEditor)o;
  73.       return ed;
  74.    }
  75.  
  76.    private static void load(Class targetType, String name) {
  77.       for(int i = 0; i < searchPath.length; ++i) {
  78.          try {
  79.             String editorName = searchPath[i] + "." + name;
  80.             Class cls = Class.forName(editorName);
  81.             registry.put(targetType, cls);
  82.             return;
  83.          }
  84.       }
  85.  
  86.       System.err.println("load of " + name + " failed");
  87.    }
  88.  
  89.    public static void registerEditor(Class targetType, Class editorClass) {
  90.       initialize();
  91.       if (editorClass == null) {
  92.          registry.remove(targetType);
  93.       } else {
  94.          registry.put(targetType, editorClass);
  95.       }
  96.  
  97.    }
  98.  
  99.    public static void setEditorSearchPath(String[] path) {
  100.       if (path == null) {
  101.          path = new String[0];
  102.       }
  103.  
  104.       searchPath = path;
  105.    }
  106. }
  107.